//
//  HarvestAddViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/25.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "HarvestAddViewController.h"
#import "HarvestVieww.h"
#import "CommodityAddVieww.h"
#import "MBProgressHUD.h"
#import "CommodityDetailViewController.h"
#import "SearchViewController.h"
#import "HarvestDetailViewController.h"
@interface HarvestAddViewController ()
{
    MBProgressHUD *hud;
    
}
@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) HarvestVieww *commodityView;
@property (nonatomic, strong) NSString *style;
@end

@implementation HarvestAddViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [self.view addSubview:_mainView];
    _mainView.backgroundColor = [UIColor whiteColor];
    self.title = @"用户检索";
    [self creatView];
    self.style = @"现金";
}

- (void)creatView
{
    self.commodityView = [[[NSBundle mainBundle] loadNibNamed:@"HarvestVieww" owner:nil options:nil] lastObject];
    _commodityView.frame = CGRectMake(0, 0, KSCREENWIDTH, 600);
    [self.mainView addSubview:_commodityView];
    [[_commodityView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        if (_commodityView.text.text.length == 0) {
            [self hud:@"请输入用户编号或手机"];
            return ;
        }
        YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Mall/searchMerchant.json" paramDic:@{@"token":TOKEN, @"user_name":_commodityView.text.text} RegisterApiMethod:RequestMethodPost];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSLog(@"%@", request.responseBody);
            
            if ([request.responseBody[@"error"] integerValue] == 0) {
                
                CommodityDetailViewController *detail = [[CommodityDetailViewController alloc]init];
                detail.str = @"商品添加";
                detail.addDic = request.responseBody[@"result"];
                [self.navigationController pushViewController:detail animated:YES];
            }else{
                [self hud:request.responseBody[@"msg"]];
            
            }
            
            
            
        } failure:nil];
        
        
        
    }];
    
    
    [[_commodityView.searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        SearchViewController *search = [[SearchViewController alloc]init];
        search.rac_subject = [RACSubject subject];
        [search.rac_subject subscribeNext:^(id x) {
            
            NSNumber *num = x;
            _commodityView.text.text = [num stringValue];
            
        }];
        
        [self.navigationController pushViewController:search animated:YES];
    }];
    
}
- (void)hud:(NSString *)str
{
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    hud = [[MBProgressHUD alloc] initWithWindow:window];
    hud.removeFromSuperViewOnHide = YES;
    
    [_mainView addSubview:hud];
    hud.labelText = str;
    hud.labelColor = [UIColor colorWithHexString:@"#A1A1AC"];
    hud.labelFont = [UIFont systemFontOfSize:15];
    hud.mode = MBProgressHUDModeCustomView;
    hud.progress = 0;
    
    [hud show:YES];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hiden) userInfo:nil repeats:NO];
}
- (void)hiden
{
    [hud hide:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
